package logic

import (
	"context"
	"errors"
	"video-guard/models/mysql"

	"video-guard/videorpc/internal/svc"
	"video-guard/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PlayNumAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPlayNumAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PlayNumAddLogic {
	return &PlayNumAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PlayNumAddLogic) PlayNumAdd(in *videorpc.PlayNumAddRequest) (*videorpc.PlayNumAddResponse, error) {
	// todo: add your logic here and delete this line

	var video mysql.VideoInfo

	err := video.FirstVideoById(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("该视频不存在")
	}

	video.PlayNum++

	err = video.PlayNumAdd(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("添加失败")
	}

	return &videorpc.PlayNumAddResponse{
		VideoId: int64(video.ID),
	}, nil
}
